﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Yeetop.PayHub
{
    /// <summary>
    /// 在线支付的基类，需要被继承。
    /// Copyright © Yeetop Inc.(www.yeetop.com)  All rights reserved. 
    /// </summary>
    /// <history>
    /// [JianTao] 2009-12-31 Created  
    /// [JianTao] 2010-01-01 Added 
    /// </history>
    public abstract class BasePayment
    {
        protected BasePayment()
        {
            Merchant = new MerchantInfo();
            ReceiveData = new ReceiveDataInfo();
        }

        /// <summary>
        /// 在线支付。
        /// </summary>
        /// <history>
        /// [JianTao] 2010-01-01 增加参数 ITrade trade
        /// </history>
        public abstract void PaySend(MerchantInfo merchant, ITradeInfo trade);

        /// <summary>
        /// 检验网关页面返回的通知，确认订单是否支付成功。
        /// </summary>
        /// <param name="checkBill"></param>
        /// <returns></returns>
        abstract protected PayStatus CheckReturnData(CheckBillDelegate checkBill);

        /// <summary>
        /// 检查网关的通知，确认订单是否成功。
        /// </summary>
        /// <param name="checkBill"></param>
        /// <returns></returns>
        abstract protected bool CheckNotifyData(CheckUpdateBillDelegate checkUpdateBill);

        abstract public PayPlatformType PayPlatform { get; }

        public MerchantInfo Merchant { protected get; set; }

        /// <summary>
        /// 支付网关返回信息。
        /// </summary>
        internal ReceiveDataInfo ReceiveData { get; set; }

        /// <summary>
        /// 网关服务器IP。可以通过检查发送通知数据的IP是否在列表中以加强安全性
        /// </summary>
        public IList<string> SafeAddress { get; protected set; }

        /// 验证发送通知服务器的IP是否在网关IP列表中，以加强安全性。默认false
        /// </summary>
        public bool ValidateNotifyHostServerAddress { get; set; }

        /// <summary>
        /// 验证网关返回信息。
        /// </summary>
        /// <returns></returns>
        public PayStatus ValidateReturn(CheckBillDelegate checkBill)
        {
            if (ValidateServerIP())
            {
                return CheckReturnData(checkBill);
            }

            return PayStatus.ServerIPError;
        }

        public bool ValidateNotify(CheckUpdateBillDelegate checkBill)
        {
            if (ValidateServerIP())
            {
                return CheckNotifyData(checkBill);
            }

            return false;
        }

        /// <summary>
        /// 验证发送通知的网关是否在安全服务器IP列表中
        /// </summary>
        private bool ValidateServerIP()
        {
            if (!ValidateNotifyHostServerAddress)
            {
                return true;
            }

            if (SafeAddress.Count < 1)
            {
                return true;
            }

            string serverIp = HttpContext.Current.Request.UserHostAddress;

            foreach (String ip in SafeAddress)
            {
                if (serverIp == ip)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 发送到支付网关前的签名。虚拟接口，使用前需要被继承。
        /// </summary>
        /// <returns></returns>
        protected virtual string SendSign()
        {
            return "";
        }

        /// <summary>
        /// 接收支付网关后的签名。虚拟接口，使用前需要被继承。
        /// </summary>
        /// <returns></returns>
        protected virtual string NotifySign()
        {
            return "";
        }

        private static string format_input = ("<input type=\"hidden\" id=\"{0}\" name=\"{0}\" value=\"{1}\">" + Environment.NewLine);
        protected static string CreateFormField(string name, string strValue)
        {
            return format_input.FormatWith(new object[] { name, strValue });
        }

        private static string format_form = "<form name=\"payform\" action=\"{0}\" method=\"POST\">{1}</form>";
        protected static string CreateForm(string content, string action)
        {
            return format_form.FormatWith(new object[] { action, content });
        }

        protected static void SubmitPayForm(string formContent)
        {
            string s = formContent + "<SCRIPT LANGUAGE=\"JavaScript\">payform.submit();</SCRIPT>";
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }

        protected static void RedirectToGateway(string url)
        {
            HttpContext.Current.Response.Redirect(url, true);
        }
    }
}
